<?php

require_once 'Entity.php';

/**
 * Pozycja w menu restauracji (potrawa/składnik). 
 *
 * @author Rafał Cupał
 * @author Przemysław Furmańczyk
 */
class MenuPosition extends Entity {
    protected $restaurant = null;
    protected $name = null;
    protected $price = null;
    protected $description = null;
    protected $available = null;
    
    public function __construct($dataBase) {
        parent::__construct($dataBase, 'menuPositions');
    }
    
    public function getRestaurantId() {
        return $this->restaurant;
    }
    
    public function setRestaurantId($newRestaurantId) {
        $this->restaurant = (int)$newRestaurantId;
    }
    
    public function getName() {
        return $this->name;
    }
    
    public function setName($newName) {
        $this->name = $newName;
    }
    
    /* Cena zapisywana jest w GROSZACH! (np. cena 251 = 2,51 zł) */
    public function getPrice() {
        return $this->price;
    }
    
    public function setPrice($newPrice) {
        $this->price = $newPrice;
    }
    
    public function getDescription() {
        return $this->description;
    }
    
    public function setDescription($newDescription) {
        $this->description = $newDescription;
    }
    
    public function isAvailable() {
        return $this->available ? true : false;
    }
    
    public function setAvailable($newAvailable) {
        $this->available = $newAvailable ? true : false;
    }
    
    public function load($rowData) {
        $this->id = $rowData['id'];
        $this->getRowData($rowData, $this->restaurant, 'restaurant');
        $this->getRowData($rowData, $this->name, 'name');
        $this->getRowData($rowData, $this->price, 'price');
        $this->getRowData($rowData, $this->description, 'description');
        $this->getRowData($rowData, $this->available, 'available');
    }
    
    public function save() {
        $rowData = array();
      
        $this->setRowData($rowData, $this->restaurant, 'restaurant');
        $this->setRowData($rowData, $this->name, 'name');
        $this->setRowData($rowData, $this->price, 'price');
        $this->setRowData($rowData, $this->description, 'description');
        $this->setRowData($rowData, $this->available, 'available');
        
        if ($this->id > 0) {
            $this->dataBase->update($this->tableName, $rowData, 
                    'id = ' . $this->id);
        }
        else {
            $this->dataBase->insert($this->tableName, $rowData);
        }
    }
}
